#!/bin/bash
#
# Original Author: Triston J. Taylor pc.wiz.tt@gmail.com
# Script Name: ttyowner;
# Domain: Unix/Linux
# Verified in: Ubuntu, gentoo, mint, and SuSe
# Keywords: unix linux bash real user-name sudo su tty pty
#
# This script finds out who is really logged in to the tty. 
# Usually called by an administration script.

# Avoid being sourced as 
return 1 2>&-;

title="${0##*/}";

function usage() {
    echo "$title -- Display the user name of the tty owner";
    echo "usage: $title";    
}

[[ $1 =~ ^(--help|--usage)$ ]] && {

    usage;
    exit;
    
} || { 

    [[ $# -ne 0 ]] && {
    
        usage >&2;
        exit 1;
        
    }
    
}

# Must be a tty.
tty -s && {
    set -- $(ps -ao user,tty,ucmd | grep 'ps');
    set -- $(who | grep $2); echo $1
}

